﻿using System.Web.Mvc;
using System.Web.UI;
using Liber.WEB;
using Liber;

namespace SoBusiness.Controllers
{
    public class CompanyController : LiberController
    {
        [NoSession]
        /// <summary>
        /// 企业门户首页
        /// </summary>
        /// <returns></returns>
        public ViewResult Index(long comId)
        {
            //TODO:针对特定企业，可以定制门户页面
            return View();
        }

        [NoSession]
        /// <summary>
        /// 企业采购首页
        /// </summary>
        /// <returns></returns>
        public ViewResult Buy()
        {
            return View();
        }

        [NoSession]
        /// <summary>
        /// 企业工程首页
        /// </summary>
        /// <returns></returns>
        public ViewResult Enginee()
        {
            return View();
        }

        [NoSession]
        public ViewResult News()
        {
            return View();
        }

        public ViewResult Complaint()
        {
            Response.Cache.SetOmitVaryStar(true);
            return View();
        }

        /// <summary>
        /// 创建新公司的页面
        /// </summary>
        /// <returns></returns>
        public ViewResult NewCompany()
        {
            return View();
        }

        /// <summary>
        /// 创建一个新的企业认证申请
        /// </summary>
        /// <returns></returns>
        public JsonResult ApplyCertify()
        {
            string name = Request.Params.TryGet("name", "");
            string address = Request.Params.TryGet("address", "");
            string phone = Request.Params.TryGet("phone", "");
            string industry = Request.Params.TryGet("industry", "");
            string website = Request.Params.TryGet("website", "");
            string orgcode = Request.Params.TryGet("orgcode", "");
            string licencecode = Request.Params.TryGet("licencecode", "");
            string logourl = "/UserFile/DefaultCompanyLogo.gif";
            string licencepic = Request.Params.TryGet("licencepic", "");
            long parentorg = Request.Params.TryGetLong("parentorg", -1);

            var rtn = API.Request("CompanyApp@NewCompany")
                       .Set("name", name)
                       .Set("address", address)
                       .Set("phone", phone)
                       .Set("industry", industry)
                       .Set("website", website)
                       .Set("orgcode", orgcode)
                       .Set("licencepic", licencepic)
                       .Set("logourl",logourl)
                       .Set("licencecode", licencecode)
                       .Set("parentorg",parentorg)
                       .WaitResult();

            return Json(rtn);
        }

        public PartialViewResult wgInfoCard(long id=-1)
        {
            return PartialView();
        }

        /// <summary>
        /// 查找符合条件的企业信息
        /// </summary>
        /// <param name="key">筛选关键字</param>
        /// <returns></returns>
        public JsonResult FindCompany(string key)
        {
            if (string.IsNullOrWhiteSpace(key))
            {
                return Json(FnResult.Failure("CompanyController", "RequiredArgs").SetError("key", "key is empty"));
            }
            var cmpnys = API.Request("CompanyApp@FindCompany")
                          .Set("key", key)
                          .WaitResult();
            return Json(cmpnys);
        }

        public JsonResult ApplyJoin(long company=-1)
        {
            return Json(API.Request("OrganizeSvc@NewApplication")
                           .Set("companyid", company)
                           .WaitResult());
        }
    }
}